package com.weixiaodong.ModelDesign.生成实例.builder模式.案例;

/**
 * 纯文本编辑器
 */
@SuppressWarnings("all")
public class TEXTBuilder extends Builder{

    private final StringBuffer buffer;

    private final int DEFAULT_SPACE = 10; // 默认的左右空格数量

    public TEXTBuilder() {
        buffer = new StringBuffer();
    }


    @Override
    public void makeTitle(String title) {
        for (int i = 0; i < DEFAULT_SPACE; i++) {
            buffer.append(" ");
        }
        buffer.append(title);
        for (int i = 0; i < DEFAULT_SPACE; i++) {
            buffer.append(" ");
        }
        buffer.append("\n"); // 换行
    }

    @Override
    public void makeString(String content) {
        for (int i = 0; i < DEFAULT_SPACE / 5; i++) {
            buffer.append(" ");
        }
        buffer.append(content);
        buffer.append("\n"); // 换行
    }

    @Override
    public void makeItems(String[] items) {
        buffer.append("\n");
        buffer.append("\n");
        buffer.append("文档条目: " + "\n");
        for (int i = 0; i < items.length; i++) {
            for (int j = 0; j < DEFAULT_SPACE / 5; j++) {
                buffer.append(" ");
            }
            buffer.append("no-" + (i + 1) + ": " + items + "\n");
        }
        buffer.append("\n");
    }

    @Override
    public void close() {
        System.out.println("=================================================" + "\n");
        System.out.println("                     编辑完毕                            ");
    }


    public String getDocument() {
        return buffer.toString();
    }
}
